package com.lvy.hczn.transmit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.constant.MqConstants;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.Pack;
import com.lvy.hczn.front.system.domain.business.PackSpu;
import com.lvy.hczn.front.system.service.PackSpuService;
import com.lvy.hczn.front.system.service.WarehouseService;
import com.lvy.hczn.mq.rocket.ParamInfo;
import com.lvy.hczn.mq.rocket.ProducerUtil;
import com.lvy.hczn.transmit.domain.dto.cteck.GoodsBag;
import com.lvy.hczn.transmit.service.GoodsBagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class GoodsBagServiceImpl implements GoodsBagService {

    @Autowired
    private PackSpuService packSpuService;

    @Autowired
    private ProducerUtil producerUtil;

    /**
     * 商品包装袋从平台同步
     *
     * @param request
     * @param info
     * @return void
     * @author yw
     * @date 2023-07-18 15:21:13
     */
    @Override
    @Transactional
    public void syncGoodsBagFromPlatform(HttpServletRequest request, ParamInfo<GoodsBag> info) {
        if (info != null && info.getList() != null && !info.getList().isEmpty()) {
            log.error("syncGoodsBagFromPlatform:{}", JSON.toJSONString(info));
            List<PackSpu> list = new ArrayList<>();
            List<GoodsBag> goodsBagList = JSON.parseArray(JSON.toJSONString(info.getList()), GoodsBag.class);
            //数据转换再保存
            goodsBagList.forEach(item -> {
                PackSpu packSpu = new PackSpu();
                BeanUtil.copyProperties(item, packSpu, false);
                if (item.getType() == 1) {
                    packSpu.setDelFlag(Constants.DEL_FLAG_INVALID);
                } else {
                    if (StrUtil.isEmpty(item.getBagId())) {
                        throw new UtilException("ID为" + item.getId() + "包装袋为空");
                    }
                    if (StrUtil.isEmpty(item.getProductCode())) {
                        throw new UtilException("ID为" + item.getId() + "商品编码为空");
                    }
                }
                packSpu.setSpuCode(item.getProductCode());
                packSpu.setSkuCode(item.getSkuCode());
                packSpu.setSpecCode(item.getSpecCode());
                packSpu.setPackId(item.getBagId());

                PackSpu packSpuModel = packSpuService.selectPackSpuById(packSpu.getId());
                if (packSpuModel != null) {
                    packSpuService.updateById(packSpu);
                } else {
                    packSpuService.insertPackSpu(packSpu);
                }
                list.add(packSpu);
            });

            ParamInfo<PackSpu> paramInfo = new ParamInfo<>();
            paramInfo.setList(list);
            try {
                producerUtil.sendAsyncMsg(MqConstants.TOPIC_BAG, null, JSON.toJSONString(paramInfo).getBytes(StandardCharsets.UTF_8), IdUtil.fastSimpleUUID());
            } catch (Exception e) {
                throw new UtilException("信息转发异常");
            }
        } else {
            log.error("syncGoodsBagFromPlatformError:{}", JSON.toJSONString(info));
            throw new UtilException("无数据信息");
        }
    }

    /**
     * 商品包装袋同步到仓库
     *
     * @param request
     * @param info
     * @return void
     * @author yw
     * @date 2023-07-18 15:22:18
     */
    @Override
    public List<PackSpu> syncGoodsBagFromTransmit(HttpServletRequest request, ParamInfo<PackSpu> info) {
        return packSpuService.list();
    }
}
